/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-13 Time: 22:23
 *
 * 我的工单
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    TouchableOpacity,
    ScrollView,
} from 'react-native';
import {connect} from 'react-redux';
import {COLOR_FOCUS, COLOR_THEME, COLOR_WHITE, FONT_COLOR_SUB_TITLE} from "../../../constant/PHColors"
import {styles} from './SelectRoomScreen.Style'
import {FetchStatus} from "../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../configuration/ReturnStatus";
import FontAwesome from "react-native-vector-icons/FontAwesome";
import AntDesign from "react-native-vector-icons/AntDesign";
import Entypo from "react-native-vector-icons/Entypo";
import HS_TouchableHighlight from "../../../hsstyle/HS_TouchableHighlight";

class SelectRoomScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle: `房间`,
    });

    // 构造
    constructor(props) {
        super(props);

        // 初始状态
        this.state = {
            room_dataSource:this.props.navigation.getParam("room_dataSource")?this.props.navigation.getParam("room_dataSource"):[],     //房间数据源
            myroom:this.props.navigation.getParam("myroom")?this.props.navigation.getParam("myroom"):[],  //已选择房间
            room_checked:[], //选中房间
            floor_checked:[], //选中楼层
            floor_open:{},  //打开关闭楼层(例:{"A楼1层":"open","A楼2层":"close"})
        };

        this.floor_c = [];

    }


    render() {
        return (
            <SafeAreaView style={styles.body}>

                <View style={[styles.body, {paddingBottom: 50}]}>
                    <ScrollView style={styles.scrollView}>
                            <View style={styles.headerContainer}>
                                <Text style={styles.headerText}>
                                    您要录入哪些房间，请选择：
                                </Text>
                            </View>
                            {
                                (this.state.myroom.length>0)
                                &&
                                (
                                    <View>
                                        <View style={styles.roomTitle}>
                                            <Text style={styles.roomTitleText}>
                                                我清洁的房间
                                            </Text>
                                        </View>
                                        {
                                            this.state.myroom.map((item, index) => {
                                                let check = "false";
                                                if (this.checkRepeat(item)||this.state.floor_checked.indexOf(item.floor_c)>=0) {
                                                    check = "true"
                                                }
                                                return this._render_myroom(item, index, check)
                                            })
                                        }
                                    </View>
                                )
                            }
                            {
                                (this.state.room_dataSource.length>0)
                                &&
                                (
                                    <View>
                                        <View style={styles.roomTitle}>
                                            <Text style={styles.roomTitleText}>
                                                其他房间
                                            </Text>
                                        </View>
                                        {
                                            this.state.room_dataSource.map((item, index) => {
                                                let check = "false";
                                                if (this.state.floor_checked.indexOf(item.floor_c)>=0) {
                                                    check = "true"
                                                }
                                                if(this.floor_c.indexOf(item.floor_c)<0){
                                                    //判断楼层重复
                                                    if(this.state.room_dataSource.length===index+1){
                                                        this.floor_c = [];
                                                    }else{
                                                        this.floor_c.push(item.floor_c);
                                                    }
                                                    return this._render_room(item, index, check)
                                                }
                                            })
                                        }
                                    </View>
                                )
                            }

                    </ScrollView>
                </View>

                <View style={styles.bodyBottom}>

                    <TouchableOpacity activeOpacity={0.8} onPress={() => this.next_step()}>
                        <View style={styles.bodyBottomRight}>
                            <Text style={styles.cellTitleText}>
                                下一步
                            </Text>
                        </View>
                    </TouchableOpacity>

                </View>

            </SafeAreaView>
        )
    }

    _render_myroom=(item, index ,check)=>{
        let _this = this;
        return (
            <HS_TouchableHighlight key={index} onPress={() => {
                let arr = _this.state.room_checked;
                let floor_arr = _this.state.floor_checked;
                if(this.checkRepeat(item)){
                    arr = this.detele_room_checked(item)
                    floor_arr.splice(floor_arr.indexOf(item.floor_c),1);
                }else{
                    arr.push(item);
                    if(this.ifAllCheck(arr,item.floor_c)){
                        floor_arr.push(item.floor_c)
                    }
                }
                _this.setState({
                    room_checked: arr,
                    floor_checked:floor_arr,
                });
            }}>
                <View style={styles.boxBody}>
                    <View style={styles.leftBox}>
                        {
                            (check === "true")
                                ?
                            (<AntDesign name={'checkcircle'} size={23} style={{color: COLOR_FOCUS, marginLeft: 10}} />)
                                :
                            (<Entypo name={'circle'} size={23} style={{color: '#b3b9ba', marginLeft: 10}} />)
                        }
                        <Text style={styles.leftText}>
                            {item.room}
                        </Text>
                    </View>
                </View>
            </HS_TouchableHighlight>
        )
    };

    _render_room=(item, index, check )=>{
        let _this = this;
        return (
            <View >
                <HS_TouchableHighlight key={index} onPress={() => {
                    const json = _this.state.floor_open;
                    if(json[item.floor_c]&&json[item.floor_c]==="open"){
                        json[item.floor_c]="close"
                    }else{
                        json[item.floor_c]="open"
                    }
                    _this.setState({
                        floor_open: json
                    });
                }}>
                    <View style={styles.boxBody}>
                        <View style={styles.leftBox}>
                            {
                                (check === "true")
                                    ?
                                (<AntDesign name={'checkcircle'} size={23} style={{color: COLOR_FOCUS, marginLeft: 10}} onPress={()=>{
                                    const arr = _this.state.floor_checked;
                                    arr.splice(arr.indexOf(item.floor_c),1);
                                    let room_check = this.detele_room_checked_by_floor_c(item.floor_c)
                                    _this.setState({
                                        floor_checked: arr,
                                        room_checked:room_check,
                                    });
                                }} />)
                                    :
                                (<Entypo name={'circle'} size={23} style={{color: '#b3b9ba', marginLeft: 10}} onPress={()=>{
                                    const arr = _this.state.floor_checked;
                                    arr.push(item.floor_c);
                                    let room_arr = this.add_room_checked_by_floor_c(item.floor_c);
                                    _this.setState({
                                        floor_checked: arr,
                                        room_checked:room_arr
                                    });
                                }} />)
                            }
                            <Text style={styles.leftText}>
                                {item.floor_c}
                            </Text>
                        </View>
                        <View style={styles.rightBox}>
                            <Entypo name={'chevron-right'} size={23} style={{color: COLOR_FOCUS, marginLeft: 10}}/>
                        </View>
                    </View>

                </HS_TouchableHighlight>
                {
                    (_this.state.floor_open[item.floor_c]&&_this.state.floor_open[item.floor_c]==='open')
                    &&
                    (
                        _this.state.room_dataSource.map((item1, index1) => {
                            let check = "false";
                            if (_this.checkRepeat(item1)||_this.state.floor_checked.indexOf(item1.floor_c)>=0) {
                                check = "true"
                            }
                            if(item1.floor_c===item.floor_c){
                                return _this._render_floor_c_open(item1, index1, check)
                            }
                        })
                    )
                }
            </View>
        )
    };

    _render_floor_c_open=(item, index ,check)=>{
        let _this = this;
        return (
            <HS_TouchableHighlight key={index} onPress={() => {
                let arr = _this.state.room_checked;
                let floor_arr = _this.state.floor_checked;
                if(this.checkRepeat(item)){
                    arr = this.detele_room_checked(item)
                    floor_arr.splice(floor_arr.indexOf(item.floor_c),1);
                }else{
                    arr.push(item);
                    if(this.ifAllCheck(arr,item.floor_c)){
                        floor_arr.push(item.floor_c)
                    }
                }
                _this.setState({
                    room_checked: arr,
                    floor_checked:floor_arr,
                });
            }}>
                <View style={styles.boxBody}>
                    <View style={[styles.leftBox,{marginLeft:30}]}>
                        {
                            (check === "true")
                                ?
                                (<AntDesign name={'checkcircle'} size={23} style={{color: COLOR_FOCUS, marginLeft: 10}} />)
                                :
                                (<Entypo name={'circle'} size={23} style={{color: '#b3b9ba', marginLeft: 10}} />)
                        }
                        <Text style={styles.leftText}>
                            {item.room}
                        </Text>
                    </View>
                </View>
            </HS_TouchableHighlight>
        )
    };

    checkRepeat=(item)=>{
        const jsonArr = this.state.room_checked;
        let result = false;
        for(let i =0;i<jsonArr.length;i++)
        {
            if(item.floor_c===jsonArr[i].floor_c && item.room===jsonArr[i].room)
            {
                result = true;
                break;
            }
        }
        return result
    };

    detele_room_checked=(item)=>{
        const jsonArr = this.state.room_checked;
        let outArr = [];
        for(let i =0;i<jsonArr.length;i++)
        {
            if(item.floor_c===jsonArr[i].floor_c && item.room===jsonArr[i].room){

            }else{
                outArr.push(jsonArr[i])
            }
        }
        return outArr
    };

    detele_room_checked_by_floor_c=(floor_c)=>{
        const jsonArr = this.state.room_checked;
        let outArr = [];
        for(let i =0;i<jsonArr.length;i++)
        {
            if(floor_c!==jsonArr[i].floor_c){
                outArr.push(jsonArr[i])
            }
        }
        return outArr
    };

    add_room_checked_by_floor_c=(floor_c)=>{
        const jsonArr = this.state.room_dataSource;
        let outArr = this.state.room_checked
        for(let i =0;i<jsonArr.length;i++)
        {
            if(floor_c===jsonArr[i].floor_c && this.checkRepeat(jsonArr[i])===false){
                outArr.push(jsonArr[i])
            }
        }
        return outArr
    };

    ifAllCheck=(room_checked_arr,floor_c)=>{
        let result = false;
        const jsonArr = this.state.room_dataSource;
        let arr_1 = [];
        for(let i =0;i<jsonArr.length;i++)
        {
            if(floor_c===jsonArr[i].floor_c){
                arr_1.push(jsonArr[i])
            }
        }
        let arr_2 = [];
        for(let i =0;i<room_checked_arr.length;i++)
        {
            if(floor_c===room_checked_arr[i].floor_c){
                arr_2.push(room_checked_arr[i])
            }
        }
        if(arr_1.length===arr_2.length){
            result = true;
        }
        return result
    };

    //下一步
    next_step=()=>{
        const result = []
        const floor_checked_arr = this.state.floor_checked;
        const room_checked_arr = this.state.room_checked;
        console.log(JSON.stringify(floor_checked_arr))
        console.log(JSON.stringify(room_checked_arr))
        for(let i =0;i<floor_checked_arr.length;i++)
        {
            result.push(floor_checked_arr[i])
        }

        for(let i =0;i<room_checked_arr.length;i++)
        {
            if(floor_checked_arr.indexOf(room_checked_arr[i].floor_c)<0){
                result.push(room_checked_arr[i].room)
            }
        }
        console.log(JSON.stringify(result))
        let parameter = {place:result.join(',')}
        if(result.length===1){
            let gno = ''
            for(let i =0;i<this.state.room_dataSource.length;i++)
            {
                if(result[0]===this.state.room_dataSource[i].room){
                    gno = (this.state.room_dataSource[i]['gno']?this.state.room_dataSource[i]['gno']:'')
                }
            }
            parameter.gno = gno
        }
        parameter.callBack = () =>this.freshScreen()
        this.props.navigation.navigate('addLinenAndBarScreen',parameter);
    };


    freshScreen=()=>{
        let {dispatch} = this.props;

        this.props.navigation.navigate('roomCleanScreen')
    }
}

const props = state => {
    return {

    }
}

export default SelectRoomScreen = connect(props)(SelectRoomScreen);